package cn.dancepeak.system.controller;

import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.system.anno.PreAuthorize;
import cn.dancepeak.system.domain.Permission;
import cn.dancepeak.system.query.PermissionQuery;
import cn.dancepeak.system.service.IPermissionServiceImpl;
import cn.dancepeak.utils.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Autowired
    private IPermissionServiceImpl permissionService;

    //1.增
    @PostMapping("/add")
    @PreAuthorize(sn = "permission:add",name ="新增权限",descs = "新增权限")
    public R<Void> add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return R.ok("新增成功");
        } catch (BusinessException e) {
            log.info("新增用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //2.根据ID改
    @PostMapping("/update")
    @PreAuthorize(sn = "permission:update",name ="修改权限",descs = "修改权限")
    public R<Void> update(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
            return R.ok("修改成功");
        } catch (BusinessException e) {
            log.info("修改用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //3.根据ID删
    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "permission:deleteOne",name ="删除权限",descs = "删除一条权限")
    public R<Void> delete(@PathVariable("id") Long id){
        try {
            permissionService.delete(id);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //4.批量删
    @PostMapping("/deleteByList")
    @PreAuthorize(sn = "permission:delete",name ="删除多条权限",descs = "删除多条权限")
    public R<Void> deleteByList(@RequestBody List<Long> ids){
        try {
            permissionService.deleteByList(ids);
            return R.ok("删除成功");
        } catch (BusinessException e) {
            log.info("删除用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //5.根据ID查
    @GetMapping("/{id}")
    @PreAuthorize(sn = "permission:getById",name ="查询权限",descs = "根据ID查询权限")
    public R<Permission> getById(@PathVariable("id") Long id){
        try {
            Permission permission = permissionService.getById(id);
            return R.ok(permission);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //6.分页查询
    @PostMapping("/page")
    @PreAuthorize(sn = "permission:page",name ="分页查询权限",descs = "分页查询权限")
    public R<PageInfo<Permission>> page(@RequestBody PermissionQuery permissionQuery){
        try {
            PageInfo<Permission> page = permissionService.page(permissionQuery);
            return R.ok(page);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //5.根据ID查
    @GetMapping("/getAll")
    @PreAuthorize(sn = "permission:getById",name ="查询权限",descs = "根据ID查询权限")
    public R<List<Permission>> getAll(){
        try {
            List<Permission> permission = permissionService.getAll();
            return R.ok(permission);
        } catch (BusinessException e) {
            log.info("查询用户异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
